package com.robot.member.service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import util.IdWorker;

import com.robot.member.dao.ConsumptionDao;
import com.robot.member.pojo.Consumption;

/**
 * consumption服务层
 *
 * @author lin
 */
@Service
public class ConsumptionService {
    @Autowired
    private ConsumptionDao consumptionDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询全部列表
     *
     * @return
     */
    public List<Consumption> findAll() {
        return consumptionDao.findAll();
    }


    /**
     * 条件查询+分页
     *
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public Page<Consumption> findSearch(Map whereMap, int page, int size) {
        Specification<Consumption> specification = createSpecification(whereMap);
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return consumptionDao.findAll(specification, pageRequest);
    }


    /**
     * 条件查询
     *
     * @param whereMap
     * @return
     */
    public List<Consumption> findSearch(Map whereMap) {
        Specification<Consumption> specification = createSpecification(whereMap);
        return consumptionDao.findAll(specification);
    }

    /**
     * 根据ID查询实体
     *
     * @param id
     * @return
     */
    public Consumption findById(String id) {
        return consumptionDao.findById(id).get();
    }

    /**
     * 根据id和memberId查询
     *
     * @param id
     * @param memberId
     * @return
     */
    public Consumption findByIdAndMemberIdAndDeletedAtIsNull(String id, String memberId) {
        return consumptionDao.findByIdAndMemberIdAndDeletedAtIsNull(id, memberId);
    }

    /**
     * 增加
     *
     * @param consumption
     */
    public void add(Consumption consumption) {
        //雪花分布式ID生成器
        consumption.setId(idWorker.nextId() + "");
        LocalDateTime now = LocalDateTime.now();
        consumption.setCreatedAt(now);
        consumption.setUpdatedAt(now);
        consumptionDao.save(consumption);
    }

    /**
     * 修改
     *
     * @param consumption
     */
    public void update(Consumption consumption) {
        consumptionDao.save(consumption);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void deleteById(String id) {
        consumptionDao.deleteById(id);
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<Consumption> createSpecification(Map searchMap) {
        return new Specification<Consumption>() {
            @Override
            public Predicate toPredicate(Root<Consumption> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                // id
                String id = "id";
                if (searchMap.get(id) != null && !"".equals(searchMap.get(id))) {
                    predicateList.add(cb.like(root.get(id).as(String.class), "%" + (String) searchMap.get(id) + "%"));
                }
                // 会员id
                String memberId = "memberId";
                if (searchMap.get(memberId) != null && !"".equals(searchMap.get(memberId))) {
                    predicateList.add(cb.like(root.get(memberId).as(String.class), "%" + (String) searchMap.get(memberId) + "%"));
                }
                // 会员名字
                String memberName = "memberName";
                if (searchMap.get(memberName) != null && !"".equals(searchMap.get(memberName))) {
                    predicateList.add(cb.like(root.get(memberName).as(String.class), "%" + (String) searchMap.get(memberName) + "%"));
                }
                // 店铺/商家id
                String storeId = "storeId";
                if (searchMap.get(storeId) != null && !"".equals(searchMap.get(storeId))) {
                    predicateList.add(cb.like(root.get(storeId).as(String.class), "%" + (String) searchMap.get(storeId) + "%"));
                }
                // 店铺/商家名称
                String storeName = "storeName";
                if (searchMap.get(storeName) != null && !"".equals(searchMap.get(storeName))) {
                    predicateList.add(cb.like(root.get(storeName).as(String.class), "%" + (String) searchMap.get(storeName) + "%"));
                }
                // 区域id,用于查询代理商
                String areaId = "areaId";
                if (searchMap.get(areaId) != null && !"".equals(searchMap.get(areaId))) {
                    predicateList.add(cb.like(root.get(areaId).as(String.class), "%" + (String) searchMap.get(areaId) + "%"));
                }
                // 0-获得 1-消费
                String type = "type";
                if (searchMap.get(type) != null && !"".equals(searchMap.get(type))) {
                    predicateList.add(cb.like(root.get(type).as(String.class), "%" + (String) searchMap.get(type) + "%"));
                }
                // 备注
                String remark = "remark";
                if (searchMap.get(remark) != null && !"".equals(searchMap.get(remark))) {
                    predicateList.add(cb.like(root.get(remark).as(String.class), "%" + (String) searchMap.get(remark) + "%"));
                }
                //创建时间倒序
                String createdAt = "createdAt";
                query.orderBy(cb.desc(root.get(createdAt)));

                //剔除软删除数据
                String deletedAt = "deletedAt";
                predicateList.add(cb.equal(root.get(deletedAt), cb.nullLiteral(LocalDateTime.class)));


                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }
}
